﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class RadiatorLoad : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				RadiatorHeatingLoad radiatorHeatingLoad = new RadiatorHeatingLoad();
				transaction = new Transaction(activeUIDocument.Document, "散热器片数调整");
				transaction.Start();
				RadiatorLoadForm radiatorLoadForm = new RadiatorLoadForm();
				radiatorLoadForm.Text = "散热器片数调整空间选择";
				if (radiatorLoadForm.ShowDialog() == DialogResult.OK)
				{
					int num = radiatorHeatingLoad.RadiatorNumModify(activeUIDocument, radiatorLoadForm);
					transaction.Commit();
                    if ((int)transaction.GetStatus() == 2)
					{
						MessageBox.Show("调整失败，存在不能调整的散热器！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						return Autodesk.Revit.UI.Result.Failed;
					}
					switch (num)
					{
					case 0:
						MessageBox.Show("调整完成！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						break;
					case 1:
						MessageBox.Show("未选择空间！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						break;
					case 2:
						MessageBox.Show("所选空间包含未经过负荷计算的空间！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						break;
					case 3:
						MessageBox.Show("调整完成，无法调整的空间已高亮显示！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						break;
					case 4:
						RevitVersionFuncs.ClearSelection(activeUIDocument.Selection);
						MessageBox.Show("调整失败，存在不能调整的散热器！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						break;
					}
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3 && (int)transaction.GetStatus() != 2)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
